<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :before-close="handleClose"
    :title="title"
    width="650px">
    <dynForm
      ref="dataForm"
      :info-form="form"
      :rules="rules"
      :search-tags="searchTags"
      :is-inline="false"
      :select-list="selectList"/>
    <span slot="footer">
      <el-button icon="el-icon-circle-close" @click="handleClose">{{
        $t("cancel")
      }}</el-button>
      <el-button
        v-btnThrottle="doSave"
        type="primary"
        icon="el-icon-receiving"
      >{{
        $t("save")
      }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import alarmActionApi from '@/api/alarm/action'
import { betterTips } from '@/utils/notification'
export default {
  name: 'MailModel',
  data() {
    return {
      dialogVisible: false,
      title: '',
      form: {},
      searchTags: [
        { label: this.$t('templateNumber'), prop: 'code', tag: 'DynSelect', style: { width: '100%' }},
        { label: this.$t('emailSubject'), prop: 'name', tag: 'DynInput' },
        { label: this.$t('description'), prop: 'description', tag: 'DynTextarea', row: 4 }
      ],
      selectList: {},
      rules: {
        code: [
          { required: true, message: this.$t('ActionCodeIsRequired'), trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    doSave() {
      this.$refs['dataForm'].$refs['dynForm'].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            alarmActionApi.doUpdate(this.$clearNull(this.form)).then(res => {
              betterTips(res, this.handleUpdate, this.handleClose)
            })
          } else {
            alarmActionApi.newAdd(this.$clearNull(this.form)).then(res => {
              betterTips(res, this.handleUpdate, this.handleClose)
            })
          }
        }
      })
    },
    handleOpen(value) {
      if (value && value.id) {
        this.title = this.$t('modify')
      } else {
        this.title = this.$t('add')
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].$refs['dynForm'].clearValidate()
      })
    },
    handleClose() {
      Object.assign(this.$data, this.$options.data.call(this))
    },
    // 触发父组件更新
    handleUpdate() {
      this.$emit('initList')
    }
  }
}
</script>
